package com.pets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pets.pojo.EquipmentManagement;
import com.pets.mapper.EquipmentManagementMapper;
import com.pets.pojo.Users;
import com.pets.service.IEquipmentManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pets.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 器材管理 服务实现类
 * </p>
 *
 * @author 小陳同學
 * @since 2024-05-13
 */
@Service
public class EquipmentManagementServiceImpl extends ServiceImpl<EquipmentManagementMapper, EquipmentManagement> implements IEquipmentManagementService {

    @Autowired
    private EquipmentManagementMapper equipmentManagementMapper;

    @Override
    public Result update(EquipmentManagement equipmentManagement) {
        int rows = equipmentManagementMapper.updateById(equipmentManagement);
        return rows > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @Override
    public Result register(EquipmentManagement equipmentManagement) {
        if(equipmentManagement.getEquipmentName() == null){
            return Result.fail("未设置器材名称");
        }
        if(equipmentManagement.getEquipmentNumber() == null){
            equipmentManagement.setEquipmentNumber(0);
        }
        return equipmentManagementMapper.insert(equipmentManagement) > 0 ? Result.success("成功") : Result.fail("失败");
    }

    @Override
    public Result deleteById(Integer id) {
        int rows = equipmentManagementMapper.deleteById(id);
        return rows > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Override
    public Result selectPage(Integer page, Integer pageSize, String equipmentName) {
        final LambdaQueryWrapper<EquipmentManagement> lqw = new LambdaQueryWrapper<>();
        lqw.like(equipmentName != null && !equipmentName.isEmpty(), EquipmentManagement::getEquipmentName, equipmentName);
        IPage<EquipmentManagement> Page = new Page<>(page, pageSize);
        IPage<EquipmentManagement> IPage = equipmentManagementMapper.selectPage(Page, lqw);
        return Result.success("查询成功", IPage);
    }

}
